import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xingsu/page/publish/viewmodel/publish_viewmodel.dart';

class PublishPage extends StatelessWidget {
  final PublishViewModel _viewModel = Get.put(PublishViewModel());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: Row(
          children: [
            IconButton(
                onPressed: () {
                  Get.back();
                },
                icon: const Icon(Icons.close)),
          ],
        ),
        actions: [
          Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              InkWell(
                onTap: _viewModel.publish,
                child: Container(
                  width: 50,
                  height: 30,
                  margin: const EdgeInsets.only(right: 15),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      color: const Color(0xFF14C4CC),
                      borderRadius: BorderRadius.circular(15)),
                  child: const Text(
                    "发布",
                    style: TextStyle(color: Colors.white),
                  ),
                ),
              )
            ],
          )
        ],
      ),
      body: ListView(
        padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 10),
        children: [
          Container(
            decoration: BoxDecoration(
                color: Get.theme.scaffoldBackgroundColor,
                borderRadius: BorderRadius.circular(10),
                boxShadow: [
                  BoxShadow(
                      color:
                          context.isDarkMode ? Colors.white12 : Colors.black12,
                      blurRadius: 10,
                      offset: Offset.zero)
                ]),
            height: Get.width * 0.6,
            child: Obx(() =>
                _viewModel.coverPath.isEmpty ? _emptyVideo() : _videoWidget()),
          ),
          const SizedBox(
            height: 20,
          ),
          Container(
            height: 40,
            alignment: Alignment.centerLeft,
            decoration: BoxDecoration(
                border: Border(
                    bottom: BorderSide(color: Get.theme.dividerTheme.color!))),
            child: TextField(
              controller: _viewModel.titleController,
              decoration: const InputDecoration(
                  border: InputBorder.none,
                  hintText: '标题（必填）',
                  hintStyle: TextStyle(
                      fontWeight: FontWeight.bold, color: Colors.grey)),
            ),
          ),
          const SizedBox(
            height: 10,
          ),
          Expanded(
              child: TextField(
            maxLines: 4,
            controller: _viewModel.descController,
            decoration: const InputDecoration(
              hintText: '视频简介',
              border: InputBorder.none,
            ),
          )),
        ],
      ),
    );
  }

  Widget _videoWidget() {
    return Stack(
      children: [
        ConstrainedBox(
          constraints: const BoxConstraints.expand(),
          child: Image.asset(
            _viewModel.coverPath.value,
            fit: BoxFit.fitHeight,
          ),
        ),
        Positioned(
            top: 15,
            left: 15,
            child: InkWell(
              onTap: _viewModel.chooseCover,
              child: Row(
                children: const [
                  Text("换封面"),
                  Icon(Icons.photo),
                ],
              ),
            ))
      ],
    );
  }

  Widget _emptyVideo() {
    return Container(
      alignment: Alignment.center,
      child: InkWell(
        onTap: _viewModel.chooseVideo,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: const [
            Icon(Icons.add),
            SizedBox(
              height: 10,
            ),
            Text("选择视频")
          ],
        ),
      ),
    );
  }
}
